using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(AudioSource))]
public class AudioManager : MonoBehaviour
{
	[SerializeField]
	protected float m_AudioPlayInterval = 0.5f;

	public const int AudioPoolSize = 5;

	private static AudioManager m_Instance;

	//private IAudioPlayer m_AudioInstance;

	private BGMPlayer m_BGMPlayer;

	private List<AudioPlayCache> m_AudioPlayCacheTimes;

	private const string _MusicToggleKey = "MusicToggle";

	private const string _SoundToggleKey = "SoundToggle";

	private const int _iFalse = 0;

	private const int _iTrue = 1;

	private static int _Music;

	private static int _Sound;

	public static bool Music
	{
		get
		{
			return _Music == 1;
		}
		set
		{
			_Music = (value ? 1 : 0);
			PlayerPrefs.SetInt("MusicToggle", _Music);
		}
	}

	public static bool Sound
	{
		get
		{
			return _Sound == 1;
		}
		set
		{
			_Sound = (value ? 1 : 0);
			PlayerPrefs.SetInt("SoundToggle", _Sound);
		}
	}

	private void OnApplicationQuit()
	{
		m_BGMPlayer.DestroySelf();
		m_BGMPlayer = null;
		//m_AudioInstance.ReleasePool();
		//m_AudioInstance = null;
		m_AudioPlayCacheTimes.Clear();
		m_Instance = null;
	}

	protected void Awake()
	{
		if (m_Instance == null)
		{
			m_Instance = this;
			//m_AudioInstance = CreateInstance();
			m_BGMPlayer = new BGMPlayer(base.gameObject);
			m_AudioPlayCacheTimes = new List<AudioPlayCache>();
			if (!PlayerPrefs.HasKey("MusicToggle"))
			{
				Music = true;
			}
			else
			{
				Music = (PlayerPrefs.GetInt("MusicToggle") == 1);
			}
			if (!PlayerPrefs.HasKey("SoundToggle"))
			{
				Sound = true;
			}
			else
			{
				Sound = (PlayerPrefs.GetInt("SoundToggle") == 1);
			}
			Object.DontDestroyOnLoad(base.gameObject);
		}
		else
		{
			UnityEngine.Object.Destroy(base.gameObject);
		}
	}

	protected void Update()
	{
		if (m_AudioPlayCacheTimes.Count == 0)
		{
			return;
		}
		int num = 0;
		while (num < m_AudioPlayCacheTimes.Count)
		{
			AudioPlayCache audioPlayCache = m_AudioPlayCacheTimes[num];
			if (audioPlayCache.time <= 0f)
			{
				m_AudioPlayCacheTimes.RemoveAt(num);
				continue;
			}
			AudioPlayCache value = m_AudioPlayCacheTimes[num];
			value.time -= Time.deltaTime;
			m_AudioPlayCacheTimes[num] = value;
			num++;
		}
	}

	public static void Load(string clipName)
	{
		if (m_Instance != null)
		{
			m_Instance._Load(clipName);
		}
	}

	public static void Unload(string clipName)
	{
		if (m_Instance != null)
		{
			//m_Instance.m_AudioInstance.Unload(clipName);
		}
	}

	public static void PlayLoopClip(string clipName)
	{
		if (m_Instance != null && Sound)
		{
			//m_Instance.m_AudioInstance.PlayLoop(clipName);
		}
	}

	public static void StopLoopClip(string clipName)
	{
		if (m_Instance != null)
		{
			//m_Instance.m_AudioInstance.Stop(clipName);
		}
	}

	public static void StopAll()
	{
		if (m_Instance != null)
		{
			//m_Instance.m_AudioInstance.PauseAll();
		}
	}

	public static void PlayClip(string clipName, float volume, bool stopPrevousAudio = false)
	{
		if (m_Instance != null && !m_Instance.ContainCache(clipName) && Sound)
		{
			volume = Mathf.Clamp01(volume);
			//m_Instance.m_AudioInstance.Play(clipName, volume);
			m_Instance.m_AudioPlayCacheTimes.Add(new AudioPlayCache
			{
				audioName = clipName,
				time = m_Instance.m_AudioPlayInterval
			});
		}
	}

	public static void SetMusic(bool value)
	{
		if (m_Instance != null)
		{
			Music = value;
			if (value)
			{
				m_Instance.m_BGMPlayer.OpenBGM();
			}
			else
			{
				m_Instance.m_BGMPlayer.CloseBGM(closeImmediate: false);
			}
		}
	}

	public static void SetSound(bool value)
	{
		if (m_Instance != null)
		{
			Sound = value;
		}
	}

	public static void PlayBGM(string bgmName, bool stopImmediate = false)
	{
		if (m_Instance != null && Music)
		{
			m_Instance.m_BGMPlayer.PlayBGM(bgmName, stopImmediate);
		}
	}

	public static void StopBGM(bool stopImmediate = false)
	{
		if (m_Instance != null)
		{
			m_Instance.m_BGMPlayer.CloseBGM(stopImmediate);
		}
	}

	private IAudioPlayer CreateInstance()
	{
		return new AndroidAudioPlayer(this);
	}

	protected bool ContainCache(string name)
	{
		int i = 0;
		for (int count = m_AudioPlayCacheTimes.Count; i < count; i++)
		{
			AudioPlayCache audioPlayCache = m_AudioPlayCacheTimes[i];
			if (audioPlayCache.audioName.CompareTo(name) == 0)
			{
				return true;
			}
		}
		return false;
	}

	protected void _Load(string clipName)
	{
		//StartCoroutine(m_AudioInstance.Load(clipName));
	}

	public static void BallhitRail(Vector3 velocity)
	{
		if (!(m_Instance == null))
		{
			float num = Mathf.Max(Mathf.Abs(velocity.x), Mathf.Abs(velocity.y), Mathf.Abs(velocity.z));
			if (num >= 20f)
			{
				PlayClip("CollideRailHigh", 1f);
			}
			else if (num >= 10f)
			{
				PlayClip("CollideRailMedium", 1f);
			}
			else if (num >= 5f)
			{
				PlayClip("CollideRailLow", 1f);
			}
			else
			{
				PlayClip("CollideRailTiny", 1f);
			}
		}
	}

	public static void BallhitBall(Vector3 velocity)
	{
		if (!(m_Instance == null))
		{
			float num = Mathf.Max(Mathf.Abs(velocity.x), Mathf.Abs(velocity.y), Mathf.Abs(velocity.z));
			if (num >= 10f)
			{
				PlayClip("CollideBallHigh", 1f);
			}
			else if (num >= 6f)
			{
				PlayClip("CollideBallMedium", 1f);
			}
			else if (num >= 2f)
			{
				PlayClip("CollideBallLow", 1f);
			}
			else
			{
				PlayClip("CollideBallTiny", 1f);
			}
		}
	}

	public static void FireBall(float powerScalar)
	{
		if (!(m_Instance == null))
		{
			if (powerScalar >= 0.8f)
			{
				PlayClip("ShootHigh", 1f);
			}
			else if (powerScalar >= 0.6f)
			{
				PlayClip("ShootMedium", 1f);
			}
			else if (powerScalar >= 0.3f)
			{
				PlayClip("ShootLow", 1f);
			}
			else
			{
				PlayClip("ShootTiny", 1f);
			}
		}
	}

	public static void PottedBall()
	{
		if (!(m_Instance == null))
		{
			PlayClip("Pocket", 1f);
		}
	}

	public static void Break()
	{
		if (!(m_Instance == null))
		{
			PlayClip("Break", 1f);
		}
	}

	public static void Explosion()
	{
		if (!(m_Instance == null))
		{
			PlayClip("Explosion", 1f);
		}
	}

	public static void Ready()
	{
		PlayClip("Ready", 1f);
	}
}
